package com.example.newdemo.tv;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.newdemo.BaseActivity;
import com.example.newdemo.R;
import com.example.newdemo.utils.LogUtils;

import java.util.ArrayList;

public class FocusMoveDemoActivity extends BaseActivity {


    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;

    ArrayList<TextView> tvList = new ArrayList<>();
    private AlertDialog m_currQueryDialog;
    private SeekBar volSeekBar;

    @Override
    protected int getLayoutId() {
        return R.layout.act_focus_move_demo;
    }

    @Override
    protected void initView() {
        Button upBtn = findViewById(R.id.btn_move_up);
        Button downBtn = findViewById(R.id.btn_move_down);


        upBtn.setOnClickListener(listener);
        downBtn.setOnClickListener(listener);


        tv1 = findViewById(R.id.tv_move_focus_1);
        tv2 = findViewById(R.id.tv_move_focus_2);
        tv3 = findViewById(R.id.tv_move_focus_3);
        tv4 = findViewById(R.id.tv_move_focus_4);

        tvList.add(tv1);
        tvList.add(tv2);
        tvList.add(tv3);
        tvList.add(tv4);



        tv1.setOnClickListener(listener);
        tv2.setOnClickListener(listener);
        tv3.setOnClickListener(listener);
        tv4.setOnClickListener(listener);

//        focusableInTouchMode
        tv1.setFocusableInTouchMode(true);
        tv2.setFocusableInTouchMode(true);
        tv3.setFocusableInTouchMode(true);
        tv4.setFocusableInTouchMode(true);

        tv1.setHovered(true);
        tv1.isFocused();

    }



    @Override
    protected void initData() {

    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int index = v.getId();
            switch (index) {
                case R.id.btn_move_up:
                    break;
                case R.id.btn_move_down:

                    break;
                case R.id.tv_move_focus_1:
                    showQueryDialog("是否确认退出练习？", true,
                            "取消", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    m_currQueryDialog.dismiss();
                                }
                            },
                            "确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    LogUtils.d("确定");
                                    m_currQueryDialog.dismiss();
                                }
                            });
                    break;
                case R.id.tv_move_focus_2:
                    showPlayCountDialog();
                    break;

                case R.id.tv_move_focus_3:
                    setVolume();
                    break;

                case R.id.tv_move_focus_4:
                    LogUtils.d("点击 ： " + index);
                    break;

            }

        }
    };


    protected void showQueryDialog(String mesage, boolean outCancel, String leftButton, View.OnClickListener leftListener,
                                   String rightButton, View.OnClickListener rightListener) {


        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        m_currQueryDialog = alertDialog;
        alertDialog.setCanceledOnTouchOutside(outCancel);
        alertDialog.show();

        Window win = alertDialog.getWindow();
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        win.setContentView(R.layout.xs_dialog_query);

        TextView tvMessage = (TextView) win.findViewById(R.id.xs_tvMessage);
        tvMessage.setText(mesage);

        final Button btnLeft = (Button) win.findViewById(R.id.xs_btnLeft);
        btnLeft.setText(leftButton);
        btnLeft.setOnClickListener(leftListener);

        Button btnRight = (Button) win.findViewById(R.id.xs_btnRight);
        btnRight.setText(rightButton);
        btnRight.setOnClickListener(rightListener);
    }


    private void showPlayCountDialog(){
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setCanceledOnTouchOutside(true);
        alertDialog.show();

        Window win = alertDialog.getWindow();
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        win.setContentView(R.layout.xs_dialog_set_play_count);
        final ImageView btnReduce = (ImageView) win.findViewById(R.id.btnReduce);   // 减号

//        if (m_TempoSelectorConfig != null && m_TempoSelectorConfig.m_DecButtonConfig != null) {
//            applyButtonConfig(btnReduce, m_TempoSelectorConfig.m_DecButtonConfig);
//        } else {
//            addEffects(btnReduce, R.mipmap.moshi_btn_subtract_nor, R.mipmap.moshi_btn_subtract_sel);
//        }

        final ImageView btnPlus = (ImageView) win.findViewById(R.id.btnPlus);   // 加号

//        if (m_TempoSelectorConfig != null && m_TempoSelectorConfig.m_IncButtonConfig != null) {
//            applyButtonConfig(btnPlus, m_TempoSelectorConfig.m_IncButtonConfig);
//        } else {
//            addEffects(btnPlus, R.mipmap.moshi_btn_plus_nor, R.mipmap.moshi_btn_plus_sel);
//        }


        Button btnCancel = (Button)win.findViewById(R.id.xs_btnCancel);
        Button btnOk = (Button)win.findViewById(R.id.xs_btnOk);
        final TextView tvCount = (TextView) win.findViewById(R.id.xs_tvCount);

//        if (m_TempoSelectorConfig != null) {
//            tvCount.setBackgroundResource(m_TempoSelectorConfig.m_SelectorTextBg);
//        }

        int playCount = 1 ;

        tvCount.setText(String.valueOf(playCount));

        TextView tvSDKVer = (TextView) win.findViewById(R.id.xs_tvSDKVer);
//        tvSDKVer.setText(XSRMService.getVersion());

        btnReduce.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String count = tvCount.getText().toString();
                int totalPlayCount = Integer.valueOf(count);
                if (totalPlayCount > 1) {
                    --totalPlayCount;
                }
                tvCount.setText(String.valueOf(totalPlayCount));
            }
        });

        btnPlus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String count = tvCount.getText().toString();
                int totalPlayCount = Integer.valueOf(count);
                if (totalPlayCount < 100) {
                    ++totalPlayCount;
                }
                tvCount.setText(String.valueOf(totalPlayCount));
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                alertDialog.dismiss();
//                removeEffect(btnReduce);
            }
        });

        btnOk.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                alertDialog.dismiss();
//                removeEffect(btnPlus);

                String count = tvCount.getText().toString();
                int totalPlayCount = Integer.valueOf(count);
//                setTotalPlayCount(totalPlayCount);
            }
        });

    }


    private void setVolume(){

        LinearLayout.LayoutParams volBtnParams = (LinearLayout.LayoutParams) tv3.getLayoutParams();

        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setCanceledOnTouchOutside(true);
        alertDialog.show();
        alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_UP)
                if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP)
                {

                    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                    int max = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 3

                    int currentProgress  = volSeekBar.getProgress();
                    int maxProgress = volSeekBar.getMax();
                    int perProgress = maxProgress / 20;
                    currentProgress = currentProgress + perProgress ;

                    int volume = (int)((double) currentProgress / 100.0 * max);
                    volSeekBar.setProgress(currentProgress);
                    am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);

                    LogUtils.d(" KEYCODE_VOLUME_DOWN   --- currentProgress = " + currentProgress + " ; volume = "
                            + volume + "; maxProcess = " + maxProgress + " ; max = " + max);


//                    dialog.dismiss();
                }
                return false;
            }
        });



        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

        Window window = alertDialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setContentView(R.layout.xs_dialog_change_volume);
        WindowManager.LayoutParams layoutParams = window.getAttributes();

        layoutParams.dimAmount=0.0f;
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        RelativeLayout layoutTop = window.findViewById(R.id.rl_seek_bar_conent);
        layoutTop.measure(w, h);
        int windowWidth = layoutTop.getMeasuredWidth();
        int windowHeight = layoutTop.getMeasuredHeight();

        layoutParams.width = windowWidth;
        layoutParams.height = windowHeight;
        layoutParams.x = (int)(volBtnParams.leftMargin + volBtnParams.width / 2.0 - windowWidth / 2.0);
//        layoutParams.y = (int)(volBtnParams.topMargin - 5 * m_ViewGeom.scaleY - windowHeight);
        window.setGravity(Gravity.LEFT | Gravity.TOP);

        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        window.setAttributes(layoutParams);

        volSeekBar = window.findViewById(R.id.vertical_seek_bar_line);

        AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int max = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 3
        int current = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        int progress = (int)((double) current / max * 100.0);
        volSeekBar.setProgress(progress);

        volSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (!fromUser) {

                    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                    int max = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 3
                    int volume = (int)((double) progress / 100.0 * max);
                    am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

}
